<?php
namespace App\Http\Controllers\Company;

use Illuminate\Support\Facades\Request;
use App\Models\CompanyComment;
use Illuminate\Support\Facades\Log;
use App\Models\ResumeComment;
/**
 *
 * @author Joker
 *        
 */
class CommentsController extends CompanyController
{
    
    public function __construct()
    {
        parent::__construct();
    }
    
    /**
     * 评论列表管理
     * @return [type] [description]
     */
    public function commentlist($type)
    {
        if(empty($this->userinfo->company_id))
            return redirect(url('/company/center/companyinfo-default'));
        
        $comment_id = Request::input('id', 0);
        if($comment_id > 0) { //删除评论
            CompanyComment::where('comment_id', '=', $comment_id)->delete();
            return redirect('/company/center/comments-' . $type);
        }
        $where_arr = ['company_id' => $this->userinfo->company_id,'to_comment_id'=>0];
        switch ($type) {
            case 'all':
                $comments = CompanyComment::where($where_arr)->paginate(15);
                break;
            case 'no':
                $comments = CompanyComment::where($where_arr)->paginate(15);
                break;
            case 'yes':
                $comments = CompanyComment::where($where_arr)->paginate(15);
                break;
            case 'resume':
                $comments = ResumeComment::where(['company_id'=>$this->userinfo->company_id,'from_company'=>1])->paginate(15);
                return view('company.resumecomment')
                    ->with('nav', $this->nav)
                    ->with('type', $type)
                    ->with('comments', $comments);
            default:
                $comments = CompanyComment::where($where_arr)->paginate(15);
                break;
        }
        
        return view('company.commentlist')
            ->with('nav', $this->nav)
            ->with('type', $type)
            ->with('comments', $comments);
    }
    
    public function reatehtml($company_id,$comment_id, $job_id)
    {
        $user_info = $this->userinfo;
        $cmts = CompanyComment::where(['company_id' => $company_id, 'to_comment_id' => $comment_id])->get();
        $html = '<div class="modal-header">';
        $html .= '<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>';
        $html .= '<h4 class="modal-title color-1" id="exampleModalLabel">';
        $html .= '<p class="color-1 fl">评论</p>';
        $html .= '<p class="color-9">' . $user_info->name . '</p>';
        $html .= '</h4></div>';
        $html .= '<div class="modal-body">';
        $html .= '<form>';
        $html .= '<div class="width-per-90 min-height-150">';
        $html .=  '  <ul>';
        foreach ($cmts as $key => $value) {
            $html .=   '   <li>';
            if($key == 0)
                $html .= '       <div class="color-4 text-align-center">' . $value->created_at . '</span>';
            if($key / 2 == 0) {
                $html .= '        <div class="text-left">';
                $html .= '          <img class="img-radius" width="60" height="60" src="' . userlogo($value->user_id,'small') . '">';
                $html .= '          <span class="padd-l-20 color-10">' . $value->content . '</span>';
                $html .= '        </div>';
            } else {
                $html .= '        <div class="text-right">';
                $html .= '          <span class="padd-r-20">' . $value->content . '</span>';
                $html .= '          <img class="img-radius" width="60" height="60" src="' . userlogo($value->user_id,'middle') . '">';
                $html .= '        </div>';
            }
    
            $html .= '      </li>';
        }
        $html .= '    </ul>';
        $html .= '  </div>';
        $html .= '  <div class="reply-cont">';
        $html .= '    <textarea class="height-115 area-text margin-t-20" id="message-text" placeholder="请您准时参加面试，如遇特殊情况请提前来电或回复邮件至：******@163.com"></textarea>';
        $html .= '  </div>';
        $html .= '</form>';
        $html .= '</div>';
        $html .= '<div class="modal-footer text-align-right">';
        $html .= '<button type="button" onclick="submitreply(' .$company_id . ',' . $comment_id . ',' . $this->userinfo->id . ',' . $job_id . ')" id="getMoreJobs" class="btn-ok">回复</button>';
        $html .= '</div>';
    
        echo $html;
    }
    
    public function reply()
    {
        $post = array_except(Request::all(), ['_token']);
        $commtent = CompanyComment::where(['company_id'=>$post['company_id'],'comment_id'=>$post['to_comment_id']])->first();
        if(empty($commtent))
        {
            return $this->formatJson('error','评论不存在！');
        }
        try{
            CompanyComment::firstOrCreate($post);
        }catch (\PDOException $e){
            Log::info(['评论回复失败'=>$e->getMessage()]);
            return $this->formatJson('error','回复失败！');
        }
        $avatar = userlogo(Request::input('user_id'), 'small');
        return $this->formatJson('success','回复成功！',$avatar);
    }
}